package cn.iocoder.yudao.module.engine.model.context;

import cn.hutool.core.util.IdUtil;
import cn.iocoder.yudao.framework.server.websocket.security.WebSocketUser;
import cn.iocoder.yudao.module.engine.enums.EnumAudioType;
import cn.iocoder.yudao.module.engine.model.aichat.AIChatQueue;
import cn.iocoder.yudao.module.engine.model.engine.EngineInputData;
import cn.iocoder.yudao.module.engine.model.engine.EngineOutputData;
import lombok.Data;

import java.time.Instant;

@Data
public class ControlEngineContext {
    private final String chatId = IdUtil.objectId();
    private final EngineSessionContext sessionCtx;
    private final WebSocketUser user;
    private final EngineInputData inputData; // 输入数据

    private final Instant startTime = Instant.now();
    private final EngineOutputData outputData = new EngineOutputData(); // 输出数据

    // 存储在redis中的状态数据
    private final EngineSynStatusCacheContext synStatusCtx;

    public ControlEngineContext(EngineSessionContext sessionCtx,
                                EngineSynStatusCacheContext synStatusCtx,
                                WebSocketUser user, EngineInputData inputData) {
        this.sessionCtx = sessionCtx;
        this.synStatusCtx = synStatusCtx;
        this.user = user;
        this.inputData = inputData;
    }

    public BillingContext getBillingContext() {
        return sessionCtx.getBillingContext();
    }

    public Instant getWebsocketStart() {
        return inputData.getWebsocketStart();
    }

    public String getWsSessionId() {
        return sessionCtx.getWsSessionId();
    }

    public String getGameMatchId() {
        return sessionCtx.getGameMatchId();
    }

    public Integer getRoleId() {
        return sessionCtx.getRoleId();
    }

    public Long getUserId() {
        return user.getUserId();
    }

    public Integer getStoryId() {
        return sessionCtx.getStoryId();
    }

    public AIChatQueue getAiChatQueue() {
        return sessionCtx.getAiChatQueue();
    }

    public String getUserText() {
        final EngineInputData.AudioText audioText = getInputData().getAudioText();
        final String userText = audioText != null ? audioText.getText() : null;
        return userText;
    }

    public boolean hasHighOrEqualPriorityAudioOutput(EnumAudioType audioType) {
        final EngineOutputData.Audio outputAudio = getOutputData().getMainAudio();
        if (outputAudio == null) {
            return false;
        }

        if (outputAudio.getAudioType().getPriority() <= audioType.getPriority()) {
            return true;
        }

        return false;
    }


}
